

'use strict';
const express=require('express');
const path=require('path');
const moment=require('moment');
const xtpl=require('xtpl');
const db=require(path.join(__dirname,"../configs/database.js"));

const base=require(path.join(__dirname,"./baseController.js"));


module.exports.teaNoweList=(req,res)=>{

   const page=parseInt(req.query.page||'1');
 const limit=parseInt(req.query.limit||'12');

 const start=(page-1)*limit;

 db.table('notice').count("*").then(function(count){

    const totalPage=count%limit==0?count/limit:parseInt(count/limit)+1;
    const totalPageArray=[];
    for(var i=1;i<=totalPage;i++){

    	totalPageArray.push(i);
    };
    
 db.table("notice").join("pre_teach on pre_notice.teach_id=pre_teach.teach_id").join("pre_class on pre_notice.class_id=pre_class.class_id").order("notice_id DESC").limit(start,limit).select().then(function(data){
   xtpl.renderFile(
         path.join(__dirname,"../views/admin/teaNowe_list.html"),
         {  
        array:data,
    	totalPage:totalPage,
        totalPageArray:totalPageArray,
        page:page
         },
         function(err,content){
              res.setHeader("Content-Type","text/html;charset=utf-8");
              res.end(content);
         }
   	);
        
    });
 });
};

module.exports.teaNoweAdd=(req,res)=>{



db.table('teach').select().then((data)=>{


  db.table('class').select().then((cData)=>{

        xtpl.renderFile(
        
        path.join(__dirname,"../views/admin/teaNowe_add.html"),
        {
        	array:data,
            crr:cData
        },
        (err,content)=>{

           if(err){
           	  return console.log(err);
           }

           res.setHeader("Content-Type","text/html;charset=utf-8");
           res.end(content);


        }

    	);
  });

});

};

module.exports.teaNoweAddData=(req,res)=>{
  let where=`notice_titel='${req.body.notice_titel}'`;
  db.table("notice").where(where).find().then(function(data){
          if(JSON.stringify(data)!="{}"){
              base.showMsg(res,"通知已经存在","/teaNowe/teaNowe_list");
              return false;
          }else{
               let data={
                  "notice_titel":req.body.notice_titel,
                  "notice_content":req.body.notice_content,
                  "notice_time":(Date.parse(moment(req.body.notice_time))/1000),
                  "teach_id":req.body.teach_id,
                  "class_id":req.body.class_id
              };
              db.table('notice').add(data).then(function(affectRows){
                     res.redirect('/teaNowe/teaNowe_list');
                     return false;
              }).catch(function(err){
              	if(err){
              		return console.log(err);
              	}
              });
          }
  });
};

module.exports.teaNoweEdite=(req,res)=>{
 let notice_id=req.params.notice_id;
 let where =`notice_id=${notice_id}`;
 //显示旧信息 老师的显示 班级的显示 通知的要显示
 db.table('notice').where(where).find().then(function(data){
   db.table('teach').select().then(function(tData){
            db.table('class').select().then(function(cData){
               data.notice_time=moment(data.notice_time*1000).format("YYYY-MM-DD");
    xtpl.renderFile(
    path.join(__dirname,"../views/admin/teaNowe_edite.html"),
    {
     noticeInfo:data,
     arr:tData,
     crr:cData
    },
    (err,content)=>{
        if(err){
        	return console.log(err);
        }
        res.setHeader("Context-Type","text/html;charset=utf-8");
        res.end(content);
    }
  	);
       });
   });
 });

};

module.exports.teaNoweEditeData=(req,res)=>{
   
     let notice_id=req.params.notice_id;
  let where=`notice_id=${notice_id}`;

  db.table('notice').where(where).find().then(function(data){
 
      if(JSON.stringify(data)=="{}"){
           
           base.showMsg(res,'通知不存在','/teaNowe/teaNowe_list');
           return false;

      }else{
          let check=`notice_titel='${req.body.notice_titel}' AND notice_id!=${notice_id}`;
          db.table('notice').where(check).find().then(function(data){
                if(JSON.stringify(data)!="{}"){
                	base.showMsg(res,'作业已经存在');
                     return false;
                }else{
                  let data={
                  "notice_titel":req.body.notice_titel,
                  "notice_content":req.body.notice_content,
                  "notice_time":(Date.parse(moment(req.body.notice_time))/1000),
                  "teach_id":req.body.teach_id,
                  "class_id":req.body.class_id
              };

              db.table('notice').where(`notice_id=${notice_id}`).update(data).then(function(affectRows){
                 res.redirect('/teaNowe/teaNowe_list');
                 return false;
              }).catch(function(err,content){
                    if(err){
                    	base.shoeMsg(res,'更新失败','/teaNowe/teaNowe_list');
                    	return false;
                    }
              });

                }

          });
      }

  });

   
};



module.exports.teaNoweDelete=(req,res)=>{
   let notice_id=req.params.notice_id||0;
   let where=`notice_id=${notice_id}`;
   db.table('notice').where(where).delete().then(
      

        (affectRows)=>{

          base.showMsg(res,'删除成功','/teaNowe/teaNowe_list');
          return false;
        }

   	).catch((err)=>{
      base.showMsg(res,"删除失败","/teaNowe/teaNowe_list");
      return false;
   	});




};